import wmi
import os
import socket
from GetUser import main
from _mainlib import sleep

def Get():
    w = wmi.WMI()
    strs = ''
    #ComputerSystem
    for CS in w.Win32_ComputerSystem():
        print('-------------------------ComputerSystem-------------------------')
        print("电脑名称: %s" %CS.Caption)
        print("使用者: %s" %CS.UserName)
        print("制造商: %s" %CS.Manufacturer)
        print("系统信息: %s" %CS.SystemFamily)
        print("工作组: %s" %CS.Workgroup)
        print("机器型号: %s" %CS.model)
        strs = strs + f'''-------------------------ComputerSystem-------------------------
    电脑名称: {CS.Caption}
    使用者: {CS.UserName}
    制造商: {CS.Manufacturer} 
    系统信息: {CS.SystemFamily} 
    工作组: {CS.Workgroup} 
    机器型号: {CS.model}'''


    #OperatingSystem
    for OS in w.Win32_OperatingSystem():
        print('-------------------------OperatingSystem-------------------------')
        print("操作系统: %s" %OS.Caption)
        print("语言版本: %s" %OS.MUILanguages)
        print("系统位数: %s" %OS.OSArchitecture)
        print("注册人: %s" %OS.RegisteredUser)
        print("系统驱动: %s" %OS.SystemDevice)
        print("系统目录: %s" %OS.SystemDirectory)
        strs = strs + f'''
    -------------------------OperatingSystem-------------------------
    操作系统: {OS.Caption} 
    语言版本: {OS.MUILanguages} 
    系统位数: {OS.OSArchitecture} 
    注册人: {OS.RegisteredUser} 
    系统驱动: {OS.SystemDevice} 
    系统目录: {OS.SystemDirectory} '''


    #NetworkAdapterConfiguration
    for address in w.Win32_NetworkAdapterConfiguration(ServiceName = "e1dexpress"):
        print('-------------------NetworkAdapterConfiguration-------------------')
        print("IP地址: %s" % address.IPAddress)
        print("MAC地址: %s" % address.MACAddress)
        print("网络描述: %s" % address.Description)
        strs = strs + f'''
    -------------------NetworkAdapterConfiguration-------------------
    IP地址: {address.IPAddress} 
    MAC地址: {address.MACAddress}
    网络描述: {address.Description}'''


    #Processor
    for processor in w.Win32_Processor():
        print("----------------------------Processor----------------------------")
        print("CPU型号: %s" % processor.Name.strip())
        print("CPU核数: %s" % processor.NumberOfCores)
        strs = strs + f'''
    ----------------------------Processor----------------------------
    CPU型号: {processor.Name.strip()} 
    CPU核数: {processor.NumberOfCores} '''


    #BIOS
    for BIOS in w.Win32_BIOS():
        print("-------------------------------BIOS-------------------------------")
        print("使用日期: %s" %BIOS.Description)
        print("主板型号: %s" %BIOS.SerialNumber)
        print("当前语言: %s" %BIOS.CurrentLanguage)
        strs = strs + f'''
    -------------------------------BIOS-------------------------------
    使用日期: {BIOS.Description}
    主板型号: {BIOS.SerialNumber}
    当前语言: {BIOS.CurrentLanguage}'''


    #PhysicalMemory
    k=1
    for memModule in w.Win32_PhysicalMemory():
        print("--------------------------PhysicalMemory--------------------------")
        totalMemSize = int(memModule.Capacity)
        print("内存厂商: %s" %memModule.Manufacturer)
        print("内存型号: %s" %memModule.PartNumber)
        print("内存大小: %.2fGB" %(totalMemSize/1024**3))
        strs = strs + f'''
    --------------------------PhysicalMemory{k}--------------------------
    内存厂商: {memModule.Manufacturer}
    内存型号: {memModule.PartNumber}
    内存大小: %.2fGB'''%(totalMemSize/1024**3)
        k += 1


    #DiskDrive
    k = 1
    for disk in w.Win32_DiskDrive():
        print("-----------------------------DiskDrive----------------------------")
        diskSize = int(disk.size)
        print("磁盘名称: %s" %disk.Caption)
        print("硬盘型号: %s" %disk.Model)
        print("磁盘大小: %.2fGB" %(diskSize/1024**3))
        
        strs = strs + f'''
    -----------------------------DiskDrive{k}----------------------------
    磁盘名称: {disk.Caption}
    硬盘型号: {disk.Model}
    磁盘大小: %.2fGB'''%(diskSize/1024**3)
        k += 1


    #VideoController
    k = 1
    for xk in w.Win32_VideoController():
        print('--------------------------VideoController-------------------------')
        print("显卡名称: %s" %xk.name)
        strs = strs + f'''
    --------------------------VideoController{k}-------------------------
    显卡名称: {xk.name}'''
        k += 1


        
    print("---------------------------ComputerName---------------------------")
    hostname = socket.gethostname()
    ip = socket.gethostbyname(hostname)
    print("计算机名称: %s" %hostname)
    print("IP地址: %s" %ip)
    strs = strs + f'''
    ---------------------------ComputerName---------------------------
    计算机名称: {hostname}
    IP地址: {ip}'''
    strs = strs + f'''
    -----------------------------AllUser------------------------------
    所有用户名: {str(main())}'''
    with open('..\\..\\ComputerConfig.cfg','w') as f:
        f.write(strs)
    sleep(10)

if __name__ == '__main__':
    Get()